from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
from pyquery import PyQuery as pq
import time
import random
def get_id():
    for name in ['鹏华']:
        for i in range(110,120):
            # time.sleep(1+random.random())
            url = 'http://www.zhangyupai.net/index.php?Hisinfo%5Bid%5D=&Hisinfo%5Bkey1%5D={}&Hisinfo%5Bpost_type%5D=%3C%3E1&Hisinfo%5Bstr_date%5D=&Hisinfo%5Bcategory%5D=&Hisinfo%5Btags%5D=&Hisinfo%5Bscores%5D=&Hisinfo%5Bmedia%5D=&Hisinfo%5Bgrp_id%5D=0&Hisinfo%5Bmedia_channel%5D=&Hisinfo%5Bfirstpost%5D=&Hisinfo%5Bgrp_copy%5D=&Hisinfo_page={}&r=hisinfo%2Fadmin'.format(name,i)
            header = {
            'Cookie':'Hm_lvt_703ac6a264dd3fdbeb458d71c429c1a1=1524700561,1524787269,1524805971,1524878203; __lnkrntdmcvrd=-1; PHPSESSID=8mv8avn0h8e72dt0loq32hcm54; YII_CSRF_TOKEN=a4f6dae434f27b0b807a864e23ed3a91f6cfbab5',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
            resp = requests.get(url,headers=header).text
            doc = pq(resp)
            ids = doc('tbody tr')
            for i in ids.items():
                id = i('td').eq(1).text()
                yield id

driver = webdriver.Chrome()
def get():
    base_url = 'http://www.zhangyupai.net'
    driver.get(base_url)
    input = driver.find_element_by_id('LoginForm_username')
    input.send_keys('opljp')
    submit = driver.find_element_by_id('LoginForm_password')
    submit.send_keys('ljp_51785136')
    submit.send_keys(Keys.ENTER)
    for i in get_id():
        url = 'http://www.zhangyupai.net/index.php?r=hisinfo/update&id={}'.format(i)
        driver.get(url)
        save = driver.find_element_by_css_selector('.buttons input')
        column = driver.find_element_by_css_selector('#Hisinfo_category').get_attribute('value')
        if column=='[]':
            continue
        time.sleep(random.random())
        save.click()
if __name__ == '__main__':
    get()



